<?php

namespace App\Models\Kms\Album;

use App\Models\BaseModel;
use App\Models\Kms\User\KmsUser;
use App\Remote\Feishu\FeishuImageRemote;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use SplFileInfo;

class AlbumResourceModel extends BaseModel
{
    use HasFactory;

    const TYPE_RESOURCE_1 = 1;
    const TYPE_RESOURCE_2 = 2;

    const TYPE_GROUP = [
        self::TYPE_RESOURCE_1 => "图片",
        self::TYPE_RESOURCE_2 => "视频"
    ];

    const  IS_IN_RECYCLE_BIN_YES   = 1;
    const  IS_IN_RECYCLE_BIN_NO    = 0;
    const  IS_IN_RECYCLE_BIN_GROUP = [
        self::IS_IN_RECYCLE_BIN_YES => "是",
        self::IS_IN_RECYCLE_BIN_NO  => "否"
    ];


    const POI_SOURCE_WG    = 1;
    const POI_SOURCE_A_MAP = 2;

    const POI_SOURCE_TEXT = [
        self::POI_SOURCE_WG    => "网规",
        self::POI_SOURCE_A_MAP => "高德",
    ];

    const POI_TYPE_WG_KF_SHOP            = "KF_SHOP";
    const POI_TYPE_WG_MALL               = "MALL";
    const POI_TYPE_WG_STREET             = "STREET";
    const POI_TYPE_WG_TOWNSHIP           = "TOWNSHIP";
    const POI_TYPE_WG_HIGH_SCHOOL        = "HIGH_SCHOOL";
    const POI_TYPE_WG_INDUSTRIAL_PARK    = "INDUSTRIAL_PARK";
    const POI_TYPE_WG_COMPLEMENT_GOODS   = "COMPLEMENT_GOODS";
    const POI_TYPE_WG_COMPETING_PRODUCTS = "COMPETING_PRODUCTS";

    const POI_TYPE_WG_TEXT = [
        self::POI_TYPE_WG_KF_SHOP            => "夸父门店",
        self::POI_TYPE_WG_MALL               => "商场",
        self::POI_TYPE_WG_STREET             => "街道",
        self::POI_TYPE_WG_TOWNSHIP           => "乡镇",
        self::POI_TYPE_WG_HIGH_SCHOOL        => "高校",
        self::POI_TYPE_WG_INDUSTRIAL_PARK    => "产业园",
        self::POI_TYPE_WG_COMPLEMENT_GOODS   => "竞品",
        self::POI_TYPE_WG_COMPETING_PRODUCTS => "互补品"
    ];

    const POI_TYPE_A_MAP_MALL           = "MALL";
    const POI_TYPE_A_MAP_FOOD_BEVERAGE  = "FOOD_BEVERAGE";
    const POI_TYPE_A_MAP_HOUSING_ESTATE = "HOUSING_ESTATE";
    const POI_TYPE_A_MAP_ENTERPRISE     = "ENTERPRISE";
    const POI_TYPE_A_MAP_PLACE_NAME     = "PLACE_NAME";

    const POI_TYPE_A_MAP_TEXT = [
        self::POI_TYPE_A_MAP_MALL           => "购物",
        self::POI_TYPE_A_MAP_FOOD_BEVERAGE  => "餐饮",
        self::POI_TYPE_A_MAP_HOUSING_ESTATE => "小区",
        self::POI_TYPE_A_MAP_ENTERPRISE     => "企业",
        self::POI_TYPE_A_MAP_PLACE_NAME     => "地名",
    ];

    const POI_TYPE_TEXT = self::POI_TYPE_WG_TEXT + self::POI_TYPE_A_MAP_TEXT;

    public $timestamps = false;

    protected $table = "kms_album_resource";


    const CRUISE_CHECK_STATUS_YES  = 1;
    const CRUISE_CHECK_STATUS_NO   = 0;
    const CRUISE_CHECK_STATUS_TEXT = [
        self::CRUISE_CHECK_STATUS_NO  => "待使用",
        self::CRUISE_CHECK_STATUS_YES => "已使用",
    ];

    /**
     * @return HasOne
     */
    public function fsUserByUsernameRel(): HasOne
    {
        return $this->hasOne(KmsUser::class, "name", "creatorUsername")->where("rmsUserId", ">", 0);
    }

    /**
     * @return HasOne
     */
    public function albumMainRel(): HasOne
    {
        return $this->hasOne(AlbumMainModel::class, "id", "mid");
    }

    /**
     * @param AlbumResourceModel $albumResourceModel
     * @param string $dir
     * @return mixed
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function getFsImgKey(AlbumResourceModel $albumResourceModel, string $dir = "album")
    {
        if (empty($albumResourceModel->fsImgKey)) {
            $saveDir    = getTmpDir("/kms/{$dir}/" . formatDateYmd(time()));
            $fileSystem = getFileSystem();
            if (!$fileSystem->isDirectory($saveDir)) {
                $fileSystem->makeDirectory($saveDir, 0777, true);
            }
            switch ($albumResourceModel->type) {
                case AlbumResourceModel::TYPE_RESOURCE_2:
                    $originalResourceUrl = $albumResourceModel->previewUrl;
                    break;
                default:
                    $originalResourceUrl = $albumResourceModel->originalUrl;
                    break;
            }
            $filePath = vsprintf("%s/%s.%s", [
                $saveDir,
                md5($originalResourceUrl),
                (new SplFileInfo($originalResourceUrl))->getExtension()
            ]);
            $fileSystem->put(
                $filePath,
                file_get_contents($originalResourceUrl)
            );
            $fsImgKey = FeishuImageRemote::uploadImage($filePath)["image_key"];
            $albumResourceModel->fsImgKey = $fsImgKey;
            $albumResourceModel->save();
        } else {
            $fsImgKey = $albumResourceModel->fsImgKey;
        }

        return $fsImgKey;
    }
}
